import 'contact_model.dart';
import 'repair_order_model.dart';

/// API响应基础模型
/// 用于统一处理所有API的响应格式
class ApiResponse<T> {
  final bool success;
  final String code;
  final String message;
  final T? data;
  final int statusCode;

  ApiResponse({
    required this.success,
    required this.code,
    required this.message,
    this.data,
    required this.statusCode,
  });

  factory ApiResponse.fromJson(Map<String, dynamic> json, T Function(dynamic)? fromJsonT) {
    return ApiResponse<T>(
      success: json['success'] ?? false,
      code: json['code']?.toString() ?? '',
      message: json['message']?.toString() ?? '',
      data: json['data'] != null && fromJsonT != null ? fromJsonT(json['data']) : json['data'],
      statusCode: json['statusCode'] ?? 0,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'success': success,
      'code': code,
      'message': message,
      'data': data,
      'statusCode': statusCode,
    };
  }

  /// 判断是否为成功响应
  bool get isSuccess => success && code == '1';

  /// 判断是否为网络错误
  bool get isNetworkError => code == 'NETWORK_ERROR';

  /// 判断是否为解析错误
  bool get isParseError => code == 'PARSE_ERROR';

  @override
  String toString() {
    return 'ApiResponse{success: $success, code: $code, message: $message, data: $data, statusCode: $statusCode}';
  }
}

/// 验证码响应模型
class VerifyCodeResponse {
  final bool success;
  final String message;
  final String? code;

  VerifyCodeResponse({
    required this.success,
    required this.message,
    this.code,
  });

  factory VerifyCodeResponse.fromJson(Map<String, dynamic> json) {
    return VerifyCodeResponse(
      success: json['success'] ?? false,
      message: json['message']?.toString() ?? '',
      code: json['code']?.toString(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'success': success,
      'message': message,
      'code': code,
    };
  }
}

/// 用户注册响应模型
class RegisterResponse {
  final bool success;
  final String message;
  final String? userId;
  final String? token;

  RegisterResponse({
    required this.success,
    required this.message,
    this.userId,
    this.token,
  });

  factory RegisterResponse.fromJson(Map<String, dynamic> json) {
    return RegisterResponse(
      success: json['success'] ?? false,
      message: json['message']?.toString() ?? '',
      userId: json['userId']?.toString(),
      token: json['token']?.toString(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'success': success,
      'message': message,
      'userId': userId,
      'token': token,
    };
  }
}

/// 用户登录响应模型
class LoginResponse {
  final bool success;
  final String message;
  final UserLoginData? userData;

  LoginResponse({
    required this.success,
    required this.message,
    this.userData,
  });

  factory LoginResponse.fromJson(Map<String, dynamic> json) {
    return LoginResponse(
      success: json['success'] ?? false,
      message: json['message']?.toString() ?? '',
      userData: json['userData'] != null ? UserLoginData.fromJson(json['userData']) : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'success': success,
      'message': message,
      'userData': userData?.toJson(),
    };
  }
}

/// 用户登录数据模型
class UserLoginData {
  final String id;
  final String username;
  final String tel;
  final String? headUrl;
  final String? token;
  final String? roleType;
  final String? shopName;
  final String? address;
  final String? isVip;
  final String? endTime;
  final String? createPushId;
  final String? isNeedDirectAddCarType;
  final String? isDirectAddItem;
  final String? isNeedDispatch;
  final String? isCanAddNewContact;
  final String? isCanDelContact;
  final String? isCanEditContact;
  final String? isCanKaiDan;
  final String? isCanSeeContactRepairs;
  final String? shopAD;
  final String? workStartTime;
  final String? workEndTime;
  final String? pics;
  final String? provinceName;
  final String? cityName;
  final String? location;
  final String? shopServiceId;
  final String? shopServiceName;

  UserLoginData({
    required this.id,
    required this.username,
    required this.tel,
    this.headUrl,
    this.token,
    this.roleType,
    this.shopName,
    this.address,
    this.isVip,
    this.endTime,
    this.createPushId,
    this.isNeedDirectAddCarType,
    this.isDirectAddItem,
    this.isNeedDispatch,
    this.isCanAddNewContact,
    this.isCanDelContact,
    this.isCanEditContact,
    this.isCanKaiDan,
    this.isCanSeeContactRepairs,
    this.shopAD,
    this.workStartTime,
    this.workEndTime,
    this.pics,
    this.provinceName,
    this.cityName,
    this.location,
    this.shopServiceId,
    this.shopServiceName,
  });

  factory UserLoginData.fromJson(Map<String, dynamic> json) {
    return UserLoginData(
      id: json['_id']?.toString() ?? '',
      username: json['username']?.toString() ?? '',
      tel: json['tel']?.toString() ?? '',
      headUrl: json['headurl']?.toString(),
      token: json['token']?.toString(),
      roleType: json['roletype']?.toString(),
      shopName: json['shopname']?.toString(),
      address: json['address']?.toString(),
      isVip: json['isVip']?.toString(),
      endTime: json['endTime']?.toString(),
      createPushId: json['createPushID']?.toString(),
      isNeedDirectAddCarType: json['isneeddirectaddcartype']?.toString(),
      isDirectAddItem: json['isdirectadditem']?.toString(),
      isNeedDispatch: json['isneeddispatch']?.toString(),
      isCanAddNewContact: json['iscanaddnewcontact']?.toString(),
      isCanDelContact: json['iscandelcontact']?.toString(),
      isCanEditContact: json['iscaneditcontact']?.toString(),
      isCanKaiDan: json['iscankaidan']?.toString(),
      isCanSeeContactRepairs: json['iscanseecontactrepairs']?.toString(),
      shopAD: json['advtip']?.toString(),
      workStartTime: json['workstarttime']?.toString(),
      workEndTime: json['workendtime']?.toString(),
      pics: json['pics']?.toString(),
      provinceName: json['provicename']?.toString(),
      cityName: json['cityname']?.toString(),
      location: json['location']?.toString(),
      shopServiceId: json['shopserviceID']?.toString(),
      shopServiceName: json['shopserviceName']?.toString(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      '_id': id,
      'username': username,
      'tel': tel,
      'headurl': headUrl,
      'token': token,
      'roletype': roleType,
      'shopname': shopName,
      'address': address,
      'isVip': isVip,
      'endTime': endTime,
      'createPushID': createPushId,
      'isneeddirectaddcartype': isNeedDirectAddCarType,
      'isdirectadditem': isDirectAddItem,
      'isneeddispatch': isNeedDispatch,
      'iscanaddnewcontact': isCanAddNewContact,
      'iscandelcontact': isCanDelContact,
      'iscaneditcontact': isCanEditContact,
      'iscankaidan': isCanKaiDan,
      'iscanseecontactrepairs': isCanSeeContactRepairs,
      'advtip': shopAD,
      'workstarttime': workStartTime,
      'workendtime': workEndTime,
      'pics': pics,
      'provicename': provinceName,
      'cityname': cityName,
      'location': location,
      'shopserviceID': shopServiceId,
      'shopserviceName': shopServiceName,
    };
  }
}

/// 客户列表响应模型
class ContactListResponse {
  final bool success;
  final String message;
  final List<Contact>? contacts;

  ContactListResponse({
    required this.success,
    required this.message,
    this.contacts,
  });

  factory ContactListResponse.fromJson(Map<String, dynamic> json) {
    return ContactListResponse(
      success: json['success'] ?? false,
      message: json['message']?.toString() ?? '',
      contacts: json['contacts'] != null 
          ? (json['contacts'] as List).map((e) => Contact.fromJson(e)).toList()
          : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'success': success,
      'message': message,
      'contacts': contacts?.map((e) => e.toJson()).toList(),
    };
  }
}

/// 工单列表响应模型
class RepairOrderListResponse {
  final bool success;
  final String message;
  final List<RepairOrder>? repairOrders;

  RepairOrderListResponse({
    required this.success,
    required this.message,
    this.repairOrders,
  });

  factory RepairOrderListResponse.fromJson(Map<String, dynamic> json) {
    return RepairOrderListResponse(
      success: json['success'] ?? false,
      message: json['message']?.toString() ?? '',
      repairOrders: json['repairOrders'] != null 
          ? (json['repairOrders'] as List).map((e) => RepairOrder.fromJson(e)).toList()
          : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'success': success,
      'message': message,
      'repairOrders': repairOrders?.map((e) => e.toJson()).toList(),
    };
  }
}